Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Saving OLE Objects To Disk The data associated with each OLE object is saved in its own file. This is a standard Basic file, opened in binary mode using the file commands that will be covered in Chapter 13. Once the file is open, call the OLE control’s SaveToFile method, passing the number of the open file as an argument. For example, for an OLE control named Ole1, the following code will save its data in a file named MYOLEFILE.DAT:
    FileNum = FreeFile
    Open “MyOleFile.Dat” For Binary as #FileNum
    Ole1.SaveToFile FileNum
    Close #FileNum
The procedure for saving an OLE object is the same for both linked and embedded OLE objects; but as mentioned previously, the nature of the saved data differs. For a linked OLE object, an image or picture of the data is saved to the file, along with the link information identifying the source data file. The object’s actual data, you’ll remember, is maintained by the server application in a file of the application’s usual format. For an embedded OLE object, the actual data is saved in the Basic file. Be aware that the OLE control does not keep track of whether or not its data is saved. If you load a new object into the control, any existing data will be overwritten without warning. It is the responsibility of the program to keep track of an OLE object’s data status—either saving it automatically or prompting the user to save when necessary. #### Retrieving OLE Objects From Disk Retrieving an OLE object from disk is essentially the reverse of the procedure used to save it. Open the data file in binary mode, then call the OLE control’s ReadFromFile method, passing the file number as an argument. Here’s an example:
    FileNum = FreeFile
    Open “MyOleFile.Dat” For Binary as #FileNum
    Ole1.ReadFromFile FileNum
    Close #FileNum
If you are unsure about using these file commands, I suggest that you read Chapter 13. #### Other OLE Control Properties, Methods, And Events For an OLE container that contains a linked object, the possibility always exists that the data in the linked file has been modified by another program (it could be the server application or another OLE container application that is linked to the file). To be sure that the OLE control displays the current version of the data, execute the OLE control’s Update method. This may not be necessary, however, depending on the OLE control’s UpdateOptions property setting. This property has three possible settings: • 0-Automatic (the default)—The OLE control is updated whenever the linked data changes. • 1-Frozen—The OLE control is updated whenever the user saves the linked document from within the server application. • 2-Manual—The OLE Client control is updated only when the Update method is invoked. If the setting is Automatic, the Update method will never be necessary. Of course, the UpdateOptions property applies only to linked OLE objects. Because embedded objects store their own data, they never need updating. You can use the OLE control’s Updated event to detect when an OLE object has been updated by the server application. This event is triggered each time the server application updates an OLE object. The syntax for this event procedure is:
    Sub OLE_Updated (Code As Integer)
The Code argument indicates how the OLE object was updated. Its possible values are as follows (the constant names in parentheses are predefined Windows constants that you can use in your code): • 0 (vbOLEChanged )—The file to which the object is linked has been modified. • 1 (vbOLESaved )—The file to which the object is linked has been saved by the server application. • 2 (vbOLEClosed )—The file to which the object is linked has been closed by the server application. • 3 (vbOLERenamed )—The file to which the object is linked has been renamed by the server application. This event procedure is frequently used to inform the program that the data in a linked OLE control has been changed since it was last saved. Code in the Updated event procedure can set a global “data changed” flag. The program can test this flag—for example, on exit—to determine if the data needs to be saved. You must declare and update this flag yourself, in code. #### Activating OLE Objects Activating an OLE object means starting its server application to manipulate the object. For an OLE control, the default method of activation is to double-click on it or to move the focus to the OLE control and press Enter. Other activation methods are possible, depending on the details of the server application and on the setting of the OLE control’s AutoActivate property. Table 10.1 shows the possible settings of this property. Table 10.1 AutoActivate property settings.
Constant | Value | Description
--- | --- | ---
vbOLEActivateManual | 0 | Manual. The object cannot be automatically activated. You must activate the object in code using the OLE control’s DoVerb method.
vbOLEActivateGetFocus | 1 | Focus. If the OLE control contains an object that supports single-click activation, the server application is activated when the OLE control receives the focus.
vbOLEActivateDoubleclick | 2 | (Default) Double Click. If the OLE control contains an object, the server application is activated when the user double-clicks on the OLE Container control or presses Enter when the control has the focus.
vbOLEActivateAuto | 3 | Automatic. If the OLE control contains an object, the server application is activated according to the object’s normal method of activation, either when the control receives the focus or when the user double-clicks on the control.
#### Putting OLE To Work This book is called the Visual Basic Programming Blue Book, but so far, all you’ve been doing in this chapter is sitting around looking at maps. Unavoidable—but hardly exciting. I could present plenty more OLE details, but you’ve already learned the important stuff—more than enough to put OLE to work in a real-world Visual Basic application. So let’s get to work and take OLE out for a spin. After all, the best way to learn is by rolling up your sleeves and trying it out. In this section, I’ll develop a basic OLE container application that demonstrates how some of the techniques presented earlier are applied in a real program. You’ll see how to create a new embedded object, how to edit an existing object, and how to save and retrieve objects on disk. OLE_DEMO.VBP consists of a single form containing only an OLE control and a menu. Other than changing the form’s Caption property to OLE Demonstration, you can leave all object properties at their default values. The two menus consist of a File menu with Save and Exit commands and an Object menu with Insert and Delete commands. Listing 10.1 presents the form’s objects and properties. Note: All of the demonstration programs in this chapter were developed using the OLE servers in Microsoft Word 97 and Microsoft Excel 97.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.